package com.chookss.home.audit.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.entity.HolidayDetailEntity;
import com.chookss.home.entity.ReimbursementDetailEntity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyEvent;
import com.chookss.tools.Utils;
import com.chookss.tools.bigImage.ImagePreview;
import com.chookss.tools.bigImage.bean.ImageInfo;
import com.chookss.view.MyRoundLayout;
import com.johnrambo.ktea.common.klogutil.KLog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HolidayImagsAdapter extends BaseQuickAdapter<HolidayDetailEntity.AnnexsBean.PictureInfoListBean, BaseViewHolderEx> {
    private Context context;
    private int width, height;
    private List<ImageInfo> imageInfoList = new ArrayList<>();
    private int type = 0;//0默认可以编辑删除1不可以编辑
    private int parent_position = 0;//父类所在下标

    public HolidayImagsAdapter(int layoutResId, Context context, @io.reactivex.annotations.Nullable List<HolidayDetailEntity.AnnexsBean.PictureInfoListBean> data, int parent_position, int type) {
        super(layoutResId, data);
        this.context = context;
        width = (Utils.getScreenWidthPixel(context) - Utils.dip2px(context, 48)) / 3;
        height = width;
        this.type = type;
        this.parent_position = parent_position;
        setUrlList(data);
    }

    public void setUrlList(List<HolidayDetailEntity.AnnexsBean.PictureInfoListBean> data) {
        for (int i = 0; i < data.size(); i++) {
            ImageInfo imageInfo = new ImageInfo();
            if (data.get(i).getPictureUrl().contains("x-oss-process")) {
                imageInfo.setThumbnailUrl(data.get(i).getPictureUrl().split("\\?")[0]);// 缩略图url
                imageInfo.setOriginUrl(data.get(i).getPictureUrl().split("\\?")[0]);// 缩略图url
            } else {
                imageInfo.setThumbnailUrl(data.get(i).getPictureUrl());// 缩略图url
                imageInfo.setOriginUrl(data.get(i).getPictureUrl());// 缩略图url
            }
            imageInfoList.add(imageInfo);
        }
    }

    @Override
    protected void convert(BaseViewHolderEx helper, HolidayDetailEntity.AnnexsBean.PictureInfoListBean item) {
        MyRoundLayout myRl = helper.getView(R.id.myRl);
        ImageView ivBg = helper.getView(R.id.ivBg);
        ImageView ivVideo = helper.getView(R.id.ivVideo);
        ImageView ivBf = helper.getView(R.id.ivBf);
        LinearLayout llDelete = helper.getView(R.id.llDelete);
        LinearLayout llAdd = helper.getView(R.id.llAdd);
        TextView tvUpload = helper.getView(R.id.tvUpload);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) myRl.getLayoutParams();
        params.height = height;
        params.width = width;
        myRl.setLayoutParams(params);
        RelativeLayout.LayoutParams llAddParams = (RelativeLayout.LayoutParams) llAdd.getLayoutParams();
        llAddParams.height = height;
        llAddParams.width = width;
        llAdd.setLayoutParams(llAddParams);
        llAdd.setVisibility(View.GONE);
        myRl.setVisibility(View.VISIBLE);
        if (type == 0) {
            llDelete.setVisibility(View.VISIBLE);
            tvUpload.setVisibility(View.GONE);
        } else {
            llDelete.setVisibility(View.GONE);
            tvUpload.setVisibility(View.GONE);
        }
        ivBf.setVisibility(View.GONE);
        ivBg.setVisibility(View.VISIBLE);
        GlideUtils.load(context, item.getPictureUrl(), ivBg, R.drawable.default_news);
        myRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == 1) {
                    if (imageInfoList.size() > 0) {
                        ImagePreview
                                .getInstance()
                                // 上下文，必须是activity，不需要担心内存泄漏，本框架已经处理好；
                                .setContext(context)
                                // 设置从第几张开始看（索引从0开始）
                                .setIndex(helper.getItemPosition())
                                .setShowCloseButton(true)
                                .setLoadStrategy(ImagePreview.LoadStrategy.NetworkAuto)
                                .setImageInfoList(imageInfoList)
                                .start();
                    }
                } else {
                    List<ImageInfo> imageInfoList = new ArrayList<>();
                    ImageInfo imageInfo = new ImageInfo();
                    imageInfo.setThumbnailUrl(item.getPictureUrl());// 缩略图url
                    imageInfo.setOriginUrl(item.getPictureUrl());// 缩略图url
                    KLog.i(item.getPictureUrl());
                    imageInfoList.add(imageInfo);
                    ImagePreview
                            .getInstance()
                            // 上下文，必须是activity，不需要担心内存泄漏，本框架已经处理好；
                            .setContext(context)
                            // 设置从第几张开始看（索引从0开始）
                            .setIndex(0)
                            .setShowCloseButton(true)
                            .setLoadStrategy(ImagePreview.LoadStrategy.NetworkAuto)
                            .setImageInfoList(imageInfoList)
                            .start();
                }
            }
        });
        llDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, String> map = new HashMap<>();
                map.put("position", helper.getItemPosition() + "");
                map.put("parent_position", parent_position + "");
                map.put("deleteHolidayPic", "deleteHolidayPic");
                EventBus.getDefault().post(new MyEvent(map));

            }
        });
    }

}
